//
//  XTHomeTableViewCell.swift
//  Weibo(Swift)
//
//  Created by 肖霆 on 16/5/15.
//  Copyright © 2016年 肖霆. All rights reserved.
//

import UIKit
import SnapKit

let margin:CGFloat = 10

class XTHomeTableViewCell: UITableViewCell {

    var toolBarTop:Constraint?
    
    var statusViewModel:XTStatusViewModel?{
        didSet{
            
            originalView.statusViewModel = statusViewModel
//            retweetView.statusViewModel = statusViewModel

            
            toolBarTop?.uninstall()
            if statusViewModel?.status?.retweeted_status != nil{
                retweetView.statusViewModel = statusViewModel
                retweetView.hidden = false
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTop = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
            }else{
                retweetView.hidden = true
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTop = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
            
            
        //给原创微博的数据源设置数据
            toolBar.statusViewModel = statusViewModel

            print(statusViewModel?.status?.pic_urls)
        }
    
    }
    
    //原创微博视图
    private lazy var originalView:XTStatusOriginalView = XTStatusOriginalView()
    //转发微博视图
    private lazy var retweetView:XTStatusRetweetView = XTStatusRetweetView()
    //点赞工具栏
    private lazy var toolBar:XTStatusToolBar = XTStatusToolBar()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   

    
    //添加控件，添加约束的方法
    private func setupUI(){
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        //************在根据谁设置约束时，就要把他添加到谁上
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(8)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolBarTop = make.top.equalTo(retweetView.snp_bottom).constraint
//            make.top.equalTo(retweetView.snp_bottom)
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(44)
        }
        //关键约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toolBar)
        }
        
    }
}
